package com.web.exiaodao.remote.controller;

import java.util.Map;
import java.util.List;

import com.web.exiaodao.business.constant.JConstant;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.exiaodao.remote.service.ApiExchangeProductsService;
import com.web.exiaodao.remote.vo.ExchangeProductsVo;

/**
 * @author zhanglin
 * @version V1.0
 * @Title: ApiExchangeProductsController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 兑换商品信息接口
 * @date 2016-11-28 21:36:08
 */
@Api(value = "ExchangeProducts", description = "兑换商品接口", position = 10)
@Controller
@RequestMapping("/api/exchangeProducts")
public class ApiExchangeProductsController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(ApiExchangeProductsController.class);

    @Autowired
    private ApiExchangeProductsService exchangeProductsService;

    /**
     * @return
     * @Title: findExchangeProductsList
     * @Description: 查询所有兑换商品信息
     */
    @ApiOperation(value = "查询所有兑换商品", notes = "查询所有兑换商品信息,区间[0,5000),[5000,10000],(10000,+&);当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findExchangeProductsList", method = RequestMethod.POST)
    public XaResult<List<ExchangeProductsVo>> findExchangeProductsList(
            @ApiParam("筛选起始条件,字段名:coinStart,默认0") @RequestParam(defaultValue = "0") Integer coinStart,
            @ApiParam("筛选结束条件,字段名:coinEnd,默认0(1:coinStart,coinEnd全是0的时候查询所有,coinEnd必须大于coinStart 2:相等且不为0,查询coinstart->无穷大)") @RequestParam(defaultValue = "0") Integer coinEnd,
            @ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
            @ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize
    ) throws BusinessException {
        XaResult<List<ExchangeProductsVo>> xr = new XaResult<List<ExchangeProductsVo>>();
        if (coinStart < 0 || coinEnd < coinStart) {
            xr.error("筛选不合法", true);
            return xr;
        }
        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'createTime',direction:'DESC'}]");
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith("{}", "search_");//不筛选
        if (coinEnd > coinStart && coinStart > 0) {//[start,end]
            filterParams = WebUitl.getParametersStartingWith(
                    "{'search_BETWEEN_needCoins':'" + coinStart + JConstant.SYMBOL_DOT + coinEnd + "'}", "search_");
        } else if (coinEnd.intValue() == coinStart.intValue() && coinStart > 0) {//(start,+&)
            filterParams = WebUitl.getParametersStartingWith(
                    "{'search_GT_needCoins':'" + coinStart + "'}", "search_");
        } else if (coinStart == 0 && coinStart == coinEnd) {
            filterParams = WebUitl.getParametersStartingWith("{}", "search_");
        } else {//[0,end)
            filterParams = WebUitl.getParametersStartingWith(
                    "{'search_LT_needCoins':'" + coinEnd + "'}", "search_");
        }
        try {
            return exchangeProductsService.findListEQStatusByFilter(XaConstant.Status.publish, filterParams, pageable);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                xr.error(e.getMessage(), true);
            } else {
                xr.error(XaConstant.Message.error, true);
            }
            LOGGER.error(XaConstant.Message.error, new Throwable(e));
        }
        return xr;
    }


    /**
     * @return
     * @Title: findExchangeProductsById
     * @Description: 根据ID查找单条实体
     * //@param modelId
     */
    @ApiOperation(value = "兑换商品详情", notes = "查询兑换商品详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
    @ResponseBody
    @RequestMapping(value = "findExchangeProductsById", method = RequestMethod.POST)
    public XaResult<ExchangeProductsVo> findExchangeProductsById(
            @ApiParam("编号,字段名:id,必填") @RequestParam(value = "id") Long id
    ) throws BusinessException {
        XaResult<ExchangeProductsVo> xr = new XaResult<ExchangeProductsVo>();
        try {
            return exchangeProductsService.findOne(id);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                xr.error(e.getMessage());
            } else {
                xr.error(XaConstant.Message.error);
            }
            LOGGER.error(XaConstant.Message.error, new Throwable(e));
        }
        return xr;
    }
}

